<template>
	<view class="device_name_layout">
		<view class="top">
			<view class="headImg" @click="chooseImage">
				<image class="img" :src="imagePath" mode="aspectFill"></image>
			</view>
		</view>

		<view class="confirm-btn" @click="setDeviceHeadImg">
				{{$t('setDeviceHead.confirm')}}
		</view>

	</view>
</template>

<script setup>
	import { ref } from 'vue';
	import {
		apiSetDeviceHeadImg
	} from '../../api/device_api';
	import{
		onLoad
	}from "@dcloudio/uni-app";
	import {
		i18nTool
	} from '../../utils/i18en';
	
	var param = {
		
	};
	onLoad((e)=>{
		let{data} = e;
		let imei = data;
		param.imei = imei;
		uni.setNavigationBarTitle({
			title:i18nTool("setDeviceHead.navTitle")
		})
	})
	const imagePath = ref("../../static/device/upload_head.png")
	const tempFilePath = ref(null);
	const chooseImage = () => {
		uni.chooseImage({
			count: 1, //默认9
			crop:{
				width:40 ,
				height:40,
				quality:80,
				
			},
			sourceType: ['album'], //从相册选择
			success: res => {
			    tempFilePath.value = res.tempFilePaths[0];
				imagePath.value = res.tempFilePaths[0];
				
			}
		});
	}

	const setDeviceHeadImg = async () => {
		param.filePath = tempFilePath.value;
		if(param.filePath  == null){
			uni.showToast({
				title: i18nTool('deviceNameHead.noHeadImgHint'),
				icon: "none",
			})
			return;
		}
		uni.showLoading({
			title:"loading...",
			mask:true
		})
		let res = await apiSetDeviceHeadImg(param);
		uni.hideLoading();
		uni.$emit("stepFive", true);
		
		let pages = getCurrentPages(); // 获取当前打开过的所有页面实例对象
		if (pages.length > 1) {
		    uni.navigateBack({
		        delta: pages.length - 1 // 返回的页面数，如果为1，则返回上一级页面
		    });
		} else {
		    uni.reLaunch({ // 如果只有一个页面，则重新打开根目录页面
		        url: '/pages/map/map'
		    });
		}
		
	}
</script>

<style lang="scss" scoped>
	.device_name_layout {

		.top {
			padding: 100rpx 40rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			.headImg {
				width: 100rpx;
				height: 100rpx;
				border-radius: 100rpx;
				border: 4rpx solid #53786C;
				.img{
					width: 100%;
					height: 100%;
					border-radius: 50%;
				}
			}
		}

		.confirm-btn {
			margin-left: 30rpx;
			margin-right: 30rpx;
			margin-top: 60rpx;
			height: 80rpx;
			color: #fff;
			background-color: #53786C;
			border-radius: 40rpx;
			@include flex-box-set(center);

		}
	}
</style>